package com.ksc;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class testAspect {

    @Pointcut("execution(* com.ksc.service.impl.*.*(..))")
    public void testPointCut() {

    }

    @Around("testPointCut()")
    public boolean testAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("before proceed---------------------------");
        Object[] args = pjp.getArgs();
        for (int i = 0; i < args.length; i++) {
            if (args[i] instanceof String) {
                args[i] = "default"; // 直接修改数组中的元素
            }
        }
        boolean proceed = (boolean) pjp.proceed(args);

        System.out.println(proceed);
        System.out.println("after proceed---------------------------");
        return proceed;
    }

    @AfterThrowing(pointcut = "testPointCut()", throwing = "ex")
    public void testAfterThrowing(JoinPoint joinPoint, Exception ex) {
        //抓取异常
        System.out.println("在方法 " + joinPoint.getSignature().getName() + " 中发生了异常");
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            System.out.println("参数: " + arg);
        }
        System.out.println("异常信息: " + ex.getMessage());
        System.out.println("after throwing---------------------------");
    }

}
